<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入类别关键字" prefix-icon="el-icon-search" v-model="search.cate_name">
      </el-input>
      <el-button class="filter-item" type="primary" plain v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <!-- <el-button class="filter-item" style="margin-left: 10px;" v-roles="'edit_category'" @click="addVisible = true" v-waves  type="primary" icon="el-icon-edit">添加主类栏目</el-button> -->
      <el-button class="filter-item" style="margin-left: 10px;" @click="addVisible = true" v-waves type="primary" icon="el-icon-edit">添加信息类别</el-button>
    </div>
    <el-table :data="listData" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">
      <el-table-column width="150px" align="center" label="排序">
        <template slot-scope="scope">
          <span>
              <el-input @change="handleBlur(scope.row, scope.row.sort, 'sort')" style="width: 80px;" class="filter-item" placeholder="排序" type='number' min="1" v-model="scope.row.sort"></el-input>
          </span>
        </template>
      </el-table-column>
      <el-table-column min-width="120px" label="信息分类名称">
        <template slot-scope="scope">
          <span>
              <el-input @change="handleBlur(scope.row, scope.row.cate_name, 'cate_name')" class="filter-item" placeholder="信息分类名称" v-model="scope.row.cate_name"></el-input>
          </span>
        </template>
      </el-table-column>
      <el-table-column min-width="150px" label="费用">
        <template slot-scope="scope">
          <span><el-input @change="handleBlur(scope.row, scope.row.fee, 'fee')" style="width: 100px;" type='number' min="0.01" step="0.01" class="filter-item" placeholder="费用" v-model="scope.row.fee"></el-input></span>
        </template>
      </el-table-column>
      <!-- //这里是操作   -->
      <el-table-column align="center" label="操作" width="80" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column class-name="status-col" label="状态" width="100">
        <template slot-scope="scope">
           <el-tag :type="scope.row.status | statusFilter">{{scope.row.status | statusName}}</el-tag>
        </template>
      </el-table-column> -->
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 弹框添加-->
    <el-dialog title="添加信息分类" :visible.sync="addVisible">
        <el-form :rules="rules" ref="tempForm" :model="addTemp" label-position="left" label-width="110px" style='width: 400px; margin-left:60px;'  class="demo-ruleForm">
          <el-form-item label="信息分类名称" prop="cate_name">
            <el-input  class="filter-item" placeholder="信息分类名称" v-model="addTemp.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="金额" prop="price">
            <el-input  class="filter-item" placeholder="金额" type='number' min="0.01" step="0.01" v-model="addTemp.price"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input  class="filter-item" placeholder="排序" type='number'  min="1" v-model="addTemp.sort"></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="addLoading" @click="handleCreate">确认添加</el-button>
      </span>
    </el-dialog>
    <!-- 弹框添加  end -->
    <!--可自定义按钮的样式、show/hide临界点、返回的位置  -->
    <!--如需文字提示，可在外部添加element的<el-tooltip></el-tooltip>元素  -->
    <el-tooltip placement="top" content="回到顶部">
      <back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
    </el-tooltip>
  </div>
</template>
<script>
import waves from '@/directive/waves' // 水波纹指令
import BackToTop from '@/components/BackToTop' // 回到顶部
import * as api from '@/api/main-api' // API

export default {
  name: 'Category',
  directives: {
    waves
  },
  components: { BackToTop },

  data() {
    return {
      // 回到顶部样式
      myBackToTopStyle: {
        right: '50px',
        bottom: '50px',
        width: '40px',
        height: '40px',
        'border-radius': '4px',
        'line-height': '45px', // 请保持与高度一致以垂直居中
        background: '#e7eaf1'// 按钮的背景颜色
      },
      // 长带参数
      listQuery: {
        page: 1,
        pagesize: 10
      },
      // 搜索
      search: {
        cate_name: ''
      },
      addTemp: {
        cate_name: '',
        price: '0', // 默认显示
        sort: '199' // 排序
      },
      // 规则
      rules: {
        cate_name: [
          { required: true, message: '请输入信息类别名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      },
      listData: [],
      listLoading: false,
      addLoading: false,
      addVisible: false,
      total: 0
    }
  },

  // 初始化数据
  created() {
    setTimeout(() => {
      this.fetchData()
    }, 20)
  },

  methods: {
    // 获取数据
    fetchData() {
      this.listLoading = true
      return new Promise((resolve, reject) => {
        api.infocateApi(this.listQuery).then(response => {
          if (response.data.code === 0) {
            this.listData = response.data.data
            this.total = parseInt(response.data.total)
          }
          this.listLoading = false
        })
      })
    },

    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.search)
      this.fetchData()
    },

    // 排序/编辑/费用
    handleBlur(row, infoval, typename) {
      var data = {
        id: row.info_cate_id,
        keys: typename,
        values: infoval
      }
      this.postDataUrl(data, 'edit')
    },

    // 添加栏目
    handleCreate() {
      this.$refs.tempForm.validate(valid => {
        if (valid) {
          this.addLoading = true
          this.postDataUrl(this.addTemp, 'add')
        } else {
          console.log('error submit!!')
          return false
        }
        this.addLoading = false
      })
    },

    // 删除操作
    handleDel(row) {
      this.$confirm('删除后将不再显示, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = {
          id: row.info_cate_id,
          keys: 'remove',
          values: '1'
        }
        api.editInfocateApi(data).then(response => {
          if (response.data.code === 0) {
            this.$root.message('删除成功', 'success')
            const index = this.listData.indexOf(row)
            this.listData.splice(index, 1)
          }
        })
      }).catch(() => {
        this.$root.message('已取消删除', 'info')
      })
    },

    // 操作编辑.删除 url
    postDataUrl(data, type) {
      api.editInfocateApi(data).then(response => {
        if (response.data.code === 0) {
          this.$root.message('操作成功', 'success')
          if (type === 'add') {
            setTimeout(() => {
              this.fetchData() // 再次请求
            }, 20)
          }
        }
        if (response.data.code !== 433) {
          this.addVisible = false
        }
      })
    },

    // 改变页条数
    handleSizeChange(val) {
      this.listQuery.pagesize = val
    },
    // 跳页
    handleCurrentChange(val) {
      this.listQuery.page = val
    }

  }

}
</script>
